<?php
require_once 'Cold/Generators.php';

class UsersController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'show_users')){
            $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'office',
                                            'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
    	$usersService = Application_Model_UsersService::getInstance();
        $usersPaginator = $usersService->getPaginatorRows();
        $pageId = $this->getRequest()->getParam('page');
        if (isset($pageId))
        {
            $usersPaginator->setCurrentPageNumber($pageId);
        }
        $this->view->paginator = $usersPaginator;
        $this->view->users = $usersPaginator->getCurrentItems();
        $registry = Zend_Registry::getInstance();
        $this->view->allRoles = Application_Model_RolesService::getInstance()->getRoles();
        if (isset($registry->currentUser))
        {
            $this->view->role = $registry->currentUser->get('role');
        }
    }

    public function addAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_user')){
            $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'office',
                                            'action' => 'index')));
            return;
        }
    	$this->_helper->layout->setLayout('layout_work');
        $formAddUser = new Application_Form_User ();
        if ($this->_request->isPost ()) {
        	$formData = $this->_request->getPost ();
			if ($formAddUser->isValid ( $formData )) {
				$user = new Application_Model_User();
				$usersService = Application_Model_UsersService::getInstance();
				$user->set('role', $formData['role']);
                $user->set('name', $formData['name']);
                $user->set('surname', $formData['surname']);
                $user->set('patronymic', $formData['patronymic']);
                $user->set('login', $formData['login']);
                $user->set('password', $formData['password']);
                $user->set('uni_code', Cold_Generators::generateNumbersString(8));
                $user->set('date_last_entry', time());
                //не обмінювати місцями !
                $user->set('date_create', time());
                $user->set('uni_session_code', null);
				$usersService->save($user);
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'users',
                                                'action' => 'index')));
                return;
			}
        }
        $this->view->formAddUser = $formAddUser;
    }

    public function delAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'remove_user')){
            $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'office',
                                            'action' => 'index')));
            return;
        }
        $delId = $this->getRequest()->getParam('id');
        if (isset($delId))
        {
            $userService = Application_Model_UsersService::getInstance();
            $userService->delById($delId);
        }
        $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'users',
                                        'action' => 'index')));
        return;
    }

    public function editAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'edit_user')){
            $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'office',
                                            'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        $userService = Application_Model_UsersService::getInstance();
        $formEditUser = new Application_Form_User();
        $formEditUser->getElement('login')->removeValidator('Db_NoRecordExists');
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formEditUser->isValid($formData))
            {
                //$user = $userService->findById($formData['id']);
                $user = new Application_Model_User();
                $user->set('id', $formData['id']);
                $user->set('role', $formData['role']);
                $user->set('name', $formData['name']);
                $user->set('surname', $formData['surname']);
                $user->set('patronymic', $formData['patronymic']);
                $user->set('login', $formData['login']);
                $user->set('password', $formData['password']);
                $userService->save($user);
                
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'users',
                                                'action' => 'index')));
                return;
            }
        } else {
            $findId = $this->getRequest()->getParam('id');
            if (isset($findId))
            {
                $res = $userService->findById($findId);
                if ($res != null)
                {
                    $formEditUser->populate($userService->mapToArray($res));
                } else {
                    $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'users',
                                                'action' => 'index')));
                    return;
                }
            } else {
                 $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'users',
                                                'action' => 'index')));
                return;
            }
        }
        
        $this->view->formEditUser = $formEditUser;
    }


}



